<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateBasicStepTables extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // 平台管理员登录信息表
        Schema::create('admin_users', function (Blueprint $table) {
            // 表结构
            $table->bigIncrements('id')->comment('自增ID');
            $table->string('code')->unique()->comment('登录账号');
            $table->string('password')->comment('登录密码');
            $table->boolean('is_admin')->default(false)->comment('身份标识 true：管理员；false：普通员工；');
            $table->rememberToken()->comment('登录token');
            $table->softDeletes();
            $table->timestamps();
        });

        // 客户（使用变压器的人或公司）基础数据表
        Schema::create('basic_customers', function (Blueprint $table) {
            // 表结构
            $table->increments('id')->comment('自增ID');
            $table->string('code')->unique()->comment('客户编号');
            $table->string('name')->comment('客户名称');
            $table->string('contact')->nullable()->comment('联系人');
            $table->string('mobile',16)->nullable()->unique()->comment('手机号码');
            $table->string('telephone')->nullable()->comment('办公电话');
            $table->string('fax')->nullable()->comment('传真');
            $table->string('email')->nullable()->comment('电子邮箱');
            $table->string('address')->nullable()->comment('客户地址');
            $table->string('contract_sign_date')->nullable()->comment('合同签订时间');
            $table->float('serve_time_limit')->nullable()->comment('服务期限');
            $table->string('inspection_date')->nullable()->comment('巡检时间');
            $table->integer('expire_remind_time')->nullable()->comment('服务到期时间提醒');
            $table->integer('inspection_remind_time')->nullable()->comment('巡检时间提醒');
            $table->softDeletes();
            $table->timestamps();
        });

        // 维护人员基础数据表
        Schema::create('basic_maintainers', function (Blueprint $table) {
            // 表结构
            $table->increments('id')->comment('自增ID');
            $table->string('code')->unique()->comment('维护人员编号');
            $table->string('name')->comment('维护人员姓名');
            $table->string('avatar')->nullable()->comment('头像');
            $table->string('mobile',16)->unique()->nullable()->comment('手机号码');
            $table->string('telephone')->nullable()->comment('办公电话');
            $table->string('email')->nullable()->comment('电子邮箱');
            $table->softDeletes();
            $table->timestamps();
        });

//        // 变电站基础数据表
//        Schema::create('basic_substations', function (Blueprint $table) {
//            // 表结构
//            $table->increments('id')->comment('自增ID');
//            $table->string('code')->unique()->comment('变电站编号');
//            $table->string('name')->comment('变电站名称');
//            $table->string('address')->nullable()->comment('变电站地址');
//            $table->string('remark')->nullable()->comment('备注');
//            $table->string('picture')->nullable()->comment('照片');
//            $table->string('customer_code')->comment('变电站所属客户的客户编码');
//            $table->softDeletes();
//            $table->timestamps();
//
//            // 外键约束
//            $table->foreign('customer_code')->references('code')->on('basic_customers')
//                ->onDelete('restrict')->onUpdate('cascade');
//        });

        // 变压器基础数据表
        Schema::create('basic_transformers', function (Blueprint $table) {
            // 表结构
            $table->increments('id')->comment('自增ID');
            $table->string('code')->unique()->comment('变压器编号');
            $table->string('model_code')->comment('变压器型号编号，自由编号');
            $table->string('customer_transformer_code')->comment('变压器客户编号');
            $table->string('customer_code')->nullable()->comment('变压器所属客户的客户编码');
            $table->string('model')->nullable()->comment('变压器型号');
            $table->float('capacity')->nullable()->comment('变压器容量');
            $table->string('address')->nullable()->comment('变压器所在位置');
            $table->string('remark')->nullable()->comment('备注');
//            $table->string('substation_code')->comment('变压器所属变电站编码');
            $table->softDeletes();
            $table->timestamps();

            // 外键约束
//            $table->foreign('substation_code')->references('code')->on('basic_substations')
//                ->onDelete('restrict')->onUpdate('cascade');

            // 外键约束
            $table->foreign('customer_code')->references('code')->on('basic_customers')
                ->onDelete('restrict')->onUpdate('cascade');
        });

//        //变电站与维护人员关联表
//        Schema::create('basic_substations_maintainers', function (Blueprint $table) {
//            $table->bigIncrements('id')->comment('自增ID');
//            $table->string('substation_code')->comment('变电站编码');
//            $table->string('maintainer_code')->comment('维护人员编码');
//            $table->softDeletes();
//            $table->timestamps();
//
//            $table->foreign('substation_code')->references('code')->on('basic_substations')
//                ->onUpdate('cascade')
//                ->onDelete('cascade');
//            $table->foreign('maintainer_code')->references('code')->on('basic_maintainers')
//                ->onUpdate('cascade')
//                ->onDelete('cascade');
//        });

        //变压器与维护人员关联表
        Schema::create('basic_transformers_maintainers', function (Blueprint $table) {
            $table->bigIncrements('id')->comment('自增ID');
            $table->string('transformer_code')->comment('变压器编码');
            $table->string('maintainer_code')->comment('维护人员编码');
            $table->softDeletes();
            $table->timestamps();

            $table->foreign('transformer_code')->references('code')->on('basic_transformers')
                ->onUpdate('cascade')
                ->onDelete('cascade');
            $table->foreign('maintainer_code')->references('code')->on('basic_maintainers')
                ->onUpdate('cascade')
                ->onDelete('cascade');
        });

        // 采集器数据表
        Schema::create('basic_collectors', function (Blueprint $table) {
            // 表结构
            $table->increments('id')->comment('自增ID');
            $table->string('code')->unique()->comment('采集器编号');
            $table->string('mac_code')->unique()->comment('采集器的mac地址');
            $table->string('model')->nullable()->comment('采集的型号');
            $table->string('transformer_code')->comment('采集的变压器的编码');
            $table->softDeletes();
            $table->timestamps();

            // 外键约束
            $table->foreign('transformer_code')->references('code')->on('basic_transformers')
                ->onDelete('restrict')->onUpdate('cascade');
        });

        // 采集器采集的回路 数据表
        Schema::create('basic_circuits', function (Blueprint $table) {
            // 表结构
            $table->increments('id')->comment('自增ID');
            $table->string('code')->unique()->comment('回路的编码');
            $table->string('name')->comment('采集的回路名');
            $table->string('collector_code')->comment('采集器的编码');
            $table->integer('state')->default(1)->comment('采集状态,1：正常；0：异常；');
            $table->softDeletes();
            $table->timestamps();

            // 外键约束
            $table->foreign('collector_code')->references('code')->on('basic_collectors')
                ->onDelete('restrict')->onUpdate('cascade');
        });

        // 采集器采集的变压器、电容柜、回路信息（温度、电流、电压） 数据表
        Schema::create('basic_collect_transformers', function (Blueprint $table) {
            // 表结构
            $table->increments('id')->comment('自增ID');
            $table->string('code')->unique()->comment('采集数据编号');
            $table->string('circuit_code')->comment('回路的编码');
            $table->integer('temperature')->nullable()->comment('温度');
            $table->integer('voltage')->nullable()->comment('电压');
            $table->integer('electric')->nullable()->comment('电流');
            $table->integer('process_state')->default(0)->comment('处理状态,1：已处理；0：未处理；2：处理中');
            $table->string('handler')->nullable()->comment('指派的处理人');
            $table->softDeletes();
            $table->timestamps();

            // 外键约束
            $table->foreign('circuit_code')->references('code')->on('basic_circuits')
                ->onDelete('restrict')->onUpdate('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('admin_users');
        Schema::dropIfExists('basic_customers');
        Schema::dropIfExists('basic_maintainers');
        Schema::dropIfExists('basic_transformers');
//        Schema::dropIfExists('basic_substations');
//        Schema::dropIfExists('basic_substations_maintainers');
        Schema::dropIfExists('basic_transformers_maintainers');
        Schema::dropIfExists('basic_collectors');
        Schema::dropIfExists('basic_circuits');
        Schema::dropIfExists('basic_collect_transformers');
    }
}
